package controller

import (
	"fmt"
	"hft/Database/admin"
	"hft/Http/Models/request"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"hft/tools"

	"github.com/gin-gonic/gin"
)

func (*Article) CreateGroup(c *gin.Context) {
	var group *admin.ArticleType
	c.ShouldBindJSON(&group)
	fmt.Println(tools.JsonParse(group))
	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.ArticleService.CreateGroup(claims.BaseClaims, *group)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*Article) EditGroup(c *gin.Context) {
	var group *admin.ArticleType
	c.ShouldBindJSON(&group)
	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.ArticleService.EditGroup(claims.BaseClaims, *group)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*Article) GetGroupList(c *gin.Context) {

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	groups, err := services.ArticleService.GetGroupList(claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(groups, c)

}

func (*Article) DeleteGroup(c *gin.Context) {

	var IDS *request.IDS

	c.ShouldBindJSON(&IDS)

	err := services.ArticleService.DeleteGroup(IDS.IDS)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}
